#include "Dynamic1Array.h"

template<typename T>
Dynamic1Array<T>::Dynamic1Array()
{
	//*initialize size of the array with the length of T-type
	m_pBuf = (T*) malloc(sizeof(T));
	//*create a T object in the memory we have created
	new(m_pBuf) T();
	m_iSize = 1;
}

template<typename T>
T& Dynamic1Array<T>::operator [](int index)
{
	if(index<0)
	{
		throw std::out_of_range("Index shouldn't be nagetive");
	}

	if(index>m_size-1)
	{
		EnlargeArray(index);
	}
	return m_pBuf[index];
}
template<typename T>
bool Dynamic1Array<T>::EnlargeArray(int iSize)
{
	m_pBuf = (string*)realloc(m_pBuf,sizeof(T)*iSize);
	for(int i = m_iSize;i<iSize;i++)
	{
		new(&m_pBuf[i]) T();
	}
	m_iSize = iSize;
	return TRUE;
}
template<typename T>
Dynamic1Array<T>::~Dynamic1Array()
{
	for(int i=0;i<m_iSize;i++)
	{
		m_pBuf[i].~T();
	}
	free(m_pBuf);
}